
//
//  MTThreeBtnView.m
//  07-美团外卖
//
//  Created by Apple on 16/10/19.
//  Copyright © 2016年 itheima. All rights reserved.
//

#import "MTThreeBtnView.h"

@interface MTThreeBtnView ()

@property (nonatomic, weak) UIView  *yellowLine;


//标记选中的按钮
@property (nonatomic, weak) UIButton *seletedBtn;
@end

@implementation MTThreeBtnView{

    NSMutableArray <UIButton *>*_btnsArray;
}

#pragma mark - 3.0点击按钮 切换小黄条
- (void)clickBtn:(UIButton *)sender
{
    
    //3.设置按钮的选中状态
    
        //3.1取消上一个按钮的状态
    self.seletedBtn.selected = NO;
        //3.2现在的按钮设置选中
    sender.selected = YES;
        //3.3替换选中按钮
    self.seletedBtn = sender;
    

    //1.获取按钮的在数组中的角标
       _selectedIndex = [_btnsArray indexOfObject:sender];
    

    
        //发送消息 给监听者
    [self sendActionsForControlEvents:UIControlEventValueChanged];
    
    
    
    [_yellowLine mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(_btnsArray[0]).offset(_selectedIndex * _btnsArray[0].bounds.size.width);
        
        
    }];
    
    //加个动画
//    [UIView animateWithDuration:.3 animations:^{
//        [self layoutIfNeeded];
//    }];

    
    //2.给控制器的 发送消息
    
    if([_delegate respondsToSelector:@selector(threeBtnView:withIndex:)]){
    
        [_delegate threeBtnView:self withIndex:_selectedIndex];
        
    }
}

#pragma mark - 2.0接收手动滚动scrol的偏移
- (void)setOffsetX:(CGFloat)offsetX
{
    _offsetX = offsetX;
    
    //MARK:1.修改小黄条的布局
    [_yellowLine mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(_btnsArray[0]).offset(offsetX);
        
        
    }];
    
        //加个动画
//    [UIView animateWithDuration:.3 animations:^{
//        [self layoutIfNeeded];
//    }];

    //MARK:2.修改按钮的选中状态
    
        //1.计算按钮的角标
    NSInteger index = offsetX/_btnsArray[0].bounds.size.width  + 0.5;
    
    
        //2.设置按钮的状态
    UIButton *btn = _btnsArray[index];
        //2.1取消上一个按钮的状态
        self.seletedBtn.selected = NO;
        //2.2现在的按钮设置选中
        btn.selected = YES;
        //2.3替换选中按钮
        self.seletedBtn = btn;
    
    
    
}


#pragma mark - 1.0界面搭建
-(void)setupUI
{
    self.backgroundColor = [UIColor whiteColor];
    
    //MARK:1.创建添加3个按钮
    
        //1.文字的集合
    NSArray <NSString *>*titleArray = @[@"验证码登录",@"密码登录"];
    
        //2.循环创建 btn
    _btnsArray = [NSMutableArray array];
    [titleArray enumerateObjectsUsingBlock:^(NSString * _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
        
        
        UIButton *btn = [UIButton cz_textButton:title fontSize:14 normalColor:0x555555 selectedColor:0xFF0000];
        
            //监听按钮事件
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        
            //设置默认选中-- 记得标记
        if(idx == 0){
        
            btn.selected = YES;
            self.seletedBtn = btn;
            
        }
        
        [self addSubview:btn];
        
        [_btnsArray addObject:btn];
    }];
    
    
    
    
        //4.小黄条
    UIView *yellowLine = [[UIView alloc]init];
    yellowLine.backgroundColor = [UIColor redColor];
    [self addSubview:yellowLine];
    
    
        //关联
    _yellowLine = yellowLine;
    
    
    
        //3.布局
                //参数1:axis :横着 /竖着
                //参数2:FixedSpacing 中间的间距
                //参数3:lead 头间距
                //参数4:tail 尾间距
    [_btnsArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    [_btnsArray mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(self);
        
    }];
    
    
    [yellowLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.bottom.width.equalTo(_btnsArray[0]);
        make.height.mas_equalTo(4);

    }];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
